import { defineComponent } from 'vue';
import { setToken } from '@/utils/auth';
import { loginByCityOa } from '@/api/auth/city-oa';
import queryString from 'query-string';
import modal from '@/plugins/modal';

export default defineComponent({
  created() {
    const { code, redirect } = this.$route.query;
    if (!code) {
      modal.msgError('code is empty');
      return;
    }
    loginByCityOa(code).then((res) => {
      if (res.data) {
        setToken(res.data);
        if (redirect) {
          const fakePath = `https://example.com${redirect}`;
          const url = new URL(fakePath);
          const query = queryString.parse(url.search);
          this.$router.replace({ path: redirect, query });
        } else {
          this.$router.replace({ path: '/' });
        }
      } else {
        modal.msgError(res.msg);
      }
    });
  },
});
